#include    "CSounds.h"

QString CSounds::song_name = "Sounds/Musics/default.mp3";

MMediaContent   *CSounds::background_playing = NULL;

QList<Phonon::MediaObject *>    CSounds::media_playing;
QList<Phonon::AudioOutput *>    CSounds::audio_playing;

qreal   CSounds::background_volume = 0.5;
qreal   CSounds::sounds_volume = 0.5;
bool    CSounds::sounds_muted = false;
bool    CSounds::background_muted = false;
bool    CSounds::is_set_muted = false;

void    CSounds::play()
{
    if (CSounds::background_playing->media_object != NULL)
        CSounds::background_playing->media_object->play();
}

void    CSounds::play(QString url)
{
    CSounds::song_name = url;
    CSounds::clear_background_playing();
    CSounds::background_playing = new MMediaContent(url);
    CSounds::background_playing->audio_output->setMuted(CSounds::background_muted);
}

void    CSounds::play_sound(QString url)
{
    new MMediaContent(url);
}

void    CSounds::pause()
{
    if (CSounds::background_playing->media_object != NULL)
        CSounds::background_playing->media_object->pause();
}

int     CSounds::play_pause()
{
    if(CSounds::background_playing == NULL)
    {
        play(CSounds::song_name);
        return (1);
    }
    else if(CSounds::background_playing->media_object->state() == Phonon::PausedState)
        play();
    else
        pause();
    return (0);
}

void    CSounds::stop()
{
    CSounds::clear_background_playing();
}

void    CSounds::stop_all()
{
    int i;

    i = 0;
    while (i < CSounds::media_playing.size())
    {
        CSounds::media_playing.at(i)->stop();
        i += 1;
    }
    if (CSounds::background_playing != NULL)
        CSounds::background_playing = NULL;
}

void    CSounds::clear_background_playing()
{
    if (CSounds::background_playing == NULL)
        return;
    if (CSounds::background_playing->media_object != NULL)
        CSounds::background_playing->media_object->stop();
    CSounds::background_playing = NULL;
}

void    CSounds::mute_background()
{
    if (CSounds::background_playing != NULL)
    {
        if (CSounds::background_playing->audio_output->isMuted())
        {
            CSounds::background_playing->audio_output->setMuted(false);
            CSounds::background_muted = false;
        }
        else
        {
            CSounds::background_playing->audio_output->setMuted(true);
            CSounds::background_muted = true;
        }
    }
}

void    CSounds::mute_sounds()
{
    int i;

    i = 0;
    while (i < CSounds::audio_playing.size())
    {
        if(CSounds::background_playing != NULL)
        {
            if(CSounds::audio_playing.at(i) != CSounds::background_playing->audio_output)
            {
                i += 1;
                continue;
            }
        }

        if (CSounds::sounds_muted)
            CSounds::audio_playing.at(i)->setMuted(false);
        else
            CSounds::audio_playing.at(i)->setMuted(true);
        i += 1;
    }
    if (CSounds::sounds_muted)
        CSounds::sounds_muted = false;
    else
        CSounds::sounds_muted = true;
}

void    CSounds::mute()
{
    if (CSounds::background_playing == NULL && CSounds::is_set_muted == false)
        CSounds::mute_sounds();
    else if (CSounds::sounds_muted && CSounds::background_playing->audio_output->isMuted())
    {
        CSounds::mute_background();

        if(CSounds::is_set_muted == false)
            CSounds::mute_sounds();
    }
    else
    {
        if (!CSounds::background_playing->audio_output->isMuted())
            CSounds::background_playing->audio_output->setMuted(true);

        if (!CSounds::sounds_muted && CSounds::is_set_muted == false)
            CSounds::mute_sounds();
    }
}

void    CSounds::update_background_volume(qreal volume)
{
    CSounds::background_volume = volume;
}
